//
//  ShangJiaoXiangQingView.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/4/11.
//  Copyright © 2019 李文强. All rights reserved.
//

#import "ShangJiaoXiangQingView.h"
#import "SYRequestAPI+Teach.h"
#import "JiaoXueZuoYeShangJiaoXiangQingModel.h"

@implementation ShangJiaoXiangQingView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame{
    if ([super initWithFrame:frame]) {
        [self initSubView];
    }
    return self;
}
- (void)initSubView{
    _dataArray = [NSMutableArray new];
    page = 0;
    pageSize = 30;
    [self addSubview:self.tableList];
    [_tableList mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(self.mas_right);
        make.top.equalTo(@0);
        make.bottom.equalTo(@0);
    }];
    
//    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(getCellClick:) name:@"TWCellNotice" object:nil];
    
    //添加上拉加载
    [_tableList headerRefreshBlock:^{
        [self->_tableList.mj_header beginRefreshing];
    }];
    [_tableList fooderRefreshBlock:^{
        [self->_tableList.mj_footer beginRefreshing];
    }];
}
- (UITableView *)tableList{
    if (!_tableList) {
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width,self.frame.size.height) style:UITableViewStylePlain];
        tableView.delegate  =self;
        tableView.dataSource = self;
        [tableView registerClass:[TWCell class] forCellReuseIdentifier:@"TWCell"];
        _headerView = [[SJXQHeaderView alloc]init];
        _headerView.frame = CGRectMake(0, 0, self.frame.size.width-20, 64);
        tableView.tableHeaderView = _headerView;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableList = tableView;
    }
    return _tableList;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TWCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TWCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[TWCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TWCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row%2 == 0) {
        cell.backgroundColor = [UIColor whiteColor];
    }else{
        cell.backgroundColor = [UIColorChange colorwithHexString:@"#F8F8F8"];
    }
    [cell reloadViewLayout];
    cell.index = (int)indexPath.row;
    JiaoXueZuoYeShangJiaoXiangQingModel *model = _dataArray[indexPath.row];
    UILabel *labelXueHaoLabel = (UILabel *)[cell viewWithTag:20];
    UILabel *labelNameLabel = (UILabel *)[cell viewWithTag:21];
    UILabel *labelGenderLabel = (UILabel *)[cell viewWithTag:22];
    UIImageView *labelDuiHaoImage = (UIImageView *)[cell viewWithTag:23];
    UILabel *labelScoreLabel = (UILabel *)[cell viewWithTag:24];
    labelXueHaoLabel.text = model.serial;
    labelNameLabel.text = model.name;
    if ([model.sex isEqualToString:@"1"]) {
        labelGenderLabel.text = @"男";
    }else if ([model.sex isEqualToString:@"2"]){
        labelGenderLabel.text = @"女";
    }else{
        labelGenderLabel.text = @"未知";
    }
    if ([model.isHand isEqualToString:@"1"]) {
        //上交
        labelDuiHaoImage.image = [UIImage imageNamed:@"duiHao"];
    }else{
        //未上交
        labelDuiHaoImage.image = [UIImage imageNamed:@"fudao_close"];
    }
    labelScoreLabel.text = model.score;
    cell.block = ^(int index) {
        JiaoXueZuoYeShangJiaoXiangQingModel *mo = self->_dataArray[index];
        [self.mainDelegate checkDetail:mo];
    };
    return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (!_titleView) {
        _titleView = [[SJXQHeaderTitleView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 44) andArray:@[@"学号",@"姓名",@"性别",@"是否上交",@"得分",@"操作"]];
        _titleView.backgroundColor = CONTENTBACKCOLOR;
    }
    _titleView.frame = CGRectMake(0, 0, self.frame.size.width, 44);
//    [_titleView reloadView];
    return _titleView;
}
- (void)getCellClick:(NSNotification *)info{
//    JiaoXueZuoYeShangJiaoXiangQingModel *model = _dataArray[]
//    int tag = [[info.userInfo objectForKey:@"index"] intValue];
//    [self.mainDelegate checkDetail:tag];
}
#pragma mark -上交详情
- (void)getShangJiaoXiangQingRequest{
    NSDictionary *parama;
    @try {
        parama = @{@"classId":_model.classId,
                                 @"paperId":_model.paperId,
                                 @"pageIndex":[NSString stringWithFormat:@"%d",page],
                                 @"pageSize":[NSString stringWithFormat:@"%d",pageSize]};
    } @catch (NSException *exception) {
        [XSInfoView showInfo:@"model参数为nil" onSubView:self];
    } @finally {
    }
    
    [MBProgressHUD mj_showMessage:nil toView:self];
    NSLog(@"上交详情参数  %@",parama);
    [SYRequestAPI sy_getHandList:parama success:^(id  _Nonnull responseObject) {
        NSLog(@"上交详情 %@",responseObject);
        NSArray *array = [responseObject objectForKey:@"data"];
        if (array.count>0) {
            self->page ++;
        }
        for (NSDictionary *dic in array) {
            JiaoXueZuoYeShangJiaoXiangQingModel *model = [JiaoXueZuoYeShangJiaoXiangQingModel yy_modelWithJSON:dic];
            [self->_dataArray addObject:model];
        }
        [self->_tableList reloadData];
        [self.tableList.mj_header endRefreshing];
        [self.tableList.mj_footer endRefreshing];
        [MBProgressHUD mj_hideHUDForView:self];
    } fail:^(NSError *error) {
        [self.tableList.mj_header endRefreshing];
        [self.tableList.mj_footer endRefreshing];
        [MBProgressHUD mj_hideHUDForView:self];
    }];
   
}
#pragma mark - 设置头部视图的值

/**

 */
- (void)setValueForHeader{
    _headerView.titleLabel.attributedText =  [[NSMutableAttributedString alloc] initWithString:_model.lessonName attributes:@{NSFontAttributeName: [UIFont fontWithName:@"PingFang-SC-Regular" size: 14],NSForegroundColorAttributeName: [UIColor colorWithRed:32/255.0 green:176/255.0 blue:151/255.0 alpha:1.0]}];
    _headerView.mentorLabel.attributedText = [[NSMutableAttributedString alloc] initWithString:_model.paperName attributes:@{NSFontAttributeName: [UIFont fontWithName:@"PingFang-SC-Regular" size: 12],NSForegroundColorAttributeName: [UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1.0]}];;
    _headerView.detailLabel.attributedText = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"应交：%@ 实交：%@",_model.totalStudents,_model.handStudents] attributes:@{NSFontAttributeName: [UIFont fontWithName:@"PingFang-SC-Regular" size: 12],NSForegroundColorAttributeName: [UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1.0]}];
    _headerView.timeLabel.attributedText = [[NSMutableAttributedString alloc] initWithString:_model.createDate attributes:@{NSFontAttributeName: [UIFont fontWithName:@"PingFang-SC-Regular" size: 12],NSForegroundColorAttributeName: [UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1.0]}];
    [self getShangJiaoXiangQingRequest];
}
@end
